package stancel.peter.smartsilencer.activities;

import java.util.List;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import stancel.peter.smartsilencer.CustomAdapter;
import stancel.peter.smartsilencer.Event;
import stancel.peter.smartsilencer.R;
import stancel.peter.smartsilencer.SmartSilencer;
import stancel.peter.smartsilencer.background.AlarmService;
import stancel.peter.smartsilencer.background.CheckingService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private SmartSilencer smartSilencer;
	private List<Event> list;
	private ArrayAdapter<Event> arrayAdapter;
	private ListView listView;
	private AdView adView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        adView = (AdView) this.findViewById(R.id.ad);
        adView.loadAd(new AdRequest());
        
        listView = (ListView) findViewById(R.id.listViewOfEvents);
		
		smartSilencer = (SmartSilencer) getApplication();
		setListView();
		registerForContextMenu(listView);
    }

    private void setListView(){
    	arrayAdapter = new CustomAdapter(this, list = smartSilencer.loadFromFile());
		listView.setAdapter(arrayAdapter);
		arrayAdapter.notifyDataSetChanged();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	setListView();
    }
    
    /**
	 * Vola sa pri stlaceni tlacidla Menu na zariadeni.
	 * Vytvara Hlavne menu.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_menu, menu);
		return true;
	}

	/**
	 * Podla vybraneho itemu v OptionsMenu spusti danu Aktivitu.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
			case (R.id.create_event): {
				Intent intent = new Intent(MainActivity.this, TimeActivity.class);
				startActivity(intent);
				return true;
			}
			case (R.id.settings): {
				Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
				startActivity(intent);
				return true;
			}
			case (R.id.about): {
				Intent intent = new Intent(MainActivity.this, AboutActivity.class);
				startActivity(intent);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Vola sa pri dlhom stlaceni vytvorenej udalosti.
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle(R.string.selected_event);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.context_menu, menu);
	}

	/**
	 * Podla zvolenej moznosti vykona operaciu nad udalostou.
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		
		AdapterView.AdapterContextMenuInfo menuInfo;
		menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int index = menuInfo.position;
		switch (item.getItemId()) {
			case (R.id.turn_on_off): {
				if(list.get(index).isTurnOn()){
					Toast.makeText(MainActivity.this, R.string.turn_off_event, Toast.LENGTH_SHORT).show();
					list.get(index).setTurnOn(false);
				} else {
					list.get(index).setTurnOn(true);
					Toast.makeText(MainActivity.this, R.string.turn_on_event, Toast.LENGTH_SHORT).show();
				}
				smartSilencer.saveListToFile(list);
				startService(new Intent(MainActivity.this, AlarmService.class));
				Bundle bundle = new Bundle();
				bundle.putBoolean("smartsilencer_alarmservice", false);
				Intent intent = new Intent(this, CheckingService.class);
				intent.putExtras(bundle);
				startService(intent);
				arrayAdapter.notifyDataSetChanged();
				return true;
			}
			case (R.id.remove_event): {
				createAlertDialog(index);
				return true;
			}
			case (R.id.modify_event): {
				Bundle bundle = new Bundle();
				bundle.putInt("smartsilencer_index",  index);
				Intent intent = new Intent(MainActivity.this, TimeActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Vytvori dialog.
	 * @param index poloha udalosti v liste
	 */
	private void createAlertDialog(final int index) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.remove_event);
		builder.setMessage(R.string.are_you_sure);
		builder.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						list.remove(index);
						smartSilencer.saveListToFile(list);
						startService(new Intent(MainActivity.this, AlarmService.class));
						arrayAdapter.notifyDataSetChanged();
					}
				});
		builder.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		builder.create().show();
	}
	
	@Override
	protected void onDestroy() {
		if (adView != null) {
			adView.destroy();
		}
		super.onDestroy();
	}
}